/**
 * UI 配置工具
 * 统一管理 UI 相关的配置和工具函数
 */

// Element Plus 主题配置
export const elementPlusConfig = {
  // 主题色配置
  colors: {
    primary: '#C8102E', // 南昌红
    success: '#4CAF50',
    warning: '#D4AF37', // 滕王阁金
    danger: '#E63946',
    info: '#1E88E5', // 赣江蓝
  },
  
  // 尺寸配置
  sizes: {
    small: 'small',
    default: 'default',
    large: 'large',
  },
  
  // 动画配置
  animations: {
    duration: {
      fast: 200,
      normal: 300,
      slow: 500,
    },
    easing: {
      ease: 'ease',
      easeIn: 'ease-in',
      easeOut: 'ease-out',
      easeInOut: 'ease-in-out',
    },
  },
}

// 响应式断点配置
export const breakpoints = {
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
  xxl: 1600,
}

// 间距系统
export const spacing = {
  xs: '4px',
  sm: '8px',
  md: '16px',
  lg: '24px',
  xl: '32px',
  '2xl': '48px',
  '3xl': '64px',
}

// 圆角系统
export const borderRadius = {
  sm: '4px',
  md: '8px',
  lg: '12px',
  xl: '16px',
  '2xl': '20px',
  full: '9999px',
}

// 阴影系统
export const shadows = {
  sm: '0 2px 8px rgba(200, 16, 46, 0.08)',
  md: '0 4px 12px rgba(200, 16, 46, 0.12)',
  lg: '0 8px 24px rgba(200, 16, 46, 0.16)',
  xl: '0 12px 32px rgba(200, 16, 46, 0.2)',
  gold: '0 4px 15px rgba(212, 175, 55, 0.3)',
}

// 渐变配置
export const gradients = {
  primary: 'linear-gradient(135deg, #C8102E 0%, #D4AF37 100%)',
  primaryReverse: 'linear-gradient(135deg, #D4AF37 0%, #C8102E 100%)',
  bg: 'linear-gradient(135deg, #E3F2FD 0%, #BBDEFB 50%, #90CAF9 100%)',
  card: 'linear-gradient(135deg, rgba(255,255,255,0.95) 0%, rgba(227,242,253,0.8) 100%)',
}

// 工具函数：获取响应式类名
export function getResponsiveClass(baseClass, breakpoint) {
  return `${baseClass}-${breakpoint}`
}

// 工具函数：生成动画样式
export function createAnimationStyle(name, duration, easing) {
  return {
    animationName: name,
    animationDuration: `${duration}ms`,
    animationTimingFunction: easing,
    animationFillMode: 'both',
  }
}

// 工具函数：检查是否为移动设备
export function isMobile() {
  if (typeof window === 'undefined') return false
  return window.innerWidth < breakpoints.md
}

// 工具函数：检查是否为平板设备
export function isTablet() {
  if (typeof window === 'undefined') return false
  return window.innerWidth >= breakpoints.md && window.innerWidth < breakpoints.lg
}

// 工具函数：检查是否为桌面设备
export function isDesktop() {
  if (typeof window === 'undefined') return false
  return window.innerWidth >= breakpoints.lg
}

// 工具函数：获取设备类型
export function getDeviceType() {
  if (isMobile()) return 'mobile'
  if (isTablet()) return 'tablet'
  return 'desktop'
}

// 工具函数：防抖
export function debounce(func, wait) {
  let timeout
  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout)
      func(...args)
    }
    clearTimeout(timeout)
    timeout = setTimeout(later, wait)
  }
}

// 工具函数：节流
export function throttle(func, limit) {
  let inThrottle
  return function executedFunction(...args) {
    if (!inThrottle) {
      func.apply(this, args)
      inThrottle = true
      setTimeout(() => (inThrottle = false), limit)
    }
  }
}

// 工具函数：平滑滚动
export function smoothScrollTo(element, to, duration) {
  if (duration <= 0) return
  const difference = to - element.scrollTop
  const perTick = (difference / duration) * 10

  setTimeout(() => {
    element.scrollTop = element.scrollTop + perTick
    if (element.scrollTop === to) return
    smoothScrollTo(element, to, duration - 10)
  }, 10)
}

// 工具函数：格式化数字（添加千分位）
export function formatNumber(num) {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

// 工具函数：格式化文件大小
export function formatFileSize(bytes) {
  if (bytes === 0) return '0 Bytes'
  const k = 1024
  const sizes = ['Bytes', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
}

// 工具函数：获取图片占位符
export function getImagePlaceholder(width = 300, height = 200, text = '') {
  return `https://via.placeholder.com/${width}x${height}?text=${encodeURIComponent(text)}`
}

// 工具函数：检查图片是否加载成功
export function checkImageLoad(url) {
  return new Promise((resolve, reject) => {
    const img = new Image()
    img.onload = () => resolve(true)
    img.onerror = () => reject(false)
    img.src = url
  })
}

// 工具函数：复制到剪贴板
export async function copyToClipboard(text) {
  try {
    if (navigator.clipboard && navigator.clipboard.writeText) {
      await navigator.clipboard.writeText(text)
      return true
    } else {
      // 降级方案
      const textArea = document.createElement('textarea')
      textArea.value = text
      textArea.style.position = 'fixed'
      textArea.style.opacity = '0'
      document.body.appendChild(textArea)
      textArea.select()
      document.execCommand('copy')
      document.body.removeChild(textArea)
      return true
    }
  } catch (error) {
    console.error('复制失败:', error)
    return false
  }
}

// 工具函数：下载文件
export function downloadFile(url, filename) {
  const link = document.createElement('a')
  link.href = url
  link.download = filename
  link.style.display = 'none'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

// 工具函数：获取随机颜色
export function getRandomColor() {
  const colors = [
    '#C8102E', // 南昌红
    '#D4AF37', // 滕王阁金
    '#1E88E5', // 赣江蓝
    '#4CAF50', // 绿色
    '#E63946', // 红色
  ]
  return colors[Math.floor(Math.random() * colors.length)]
}

// 工具函数：生成唯一ID
export function generateId(prefix = 'id') {
  return `${prefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}

// 工具函数：格式化日期
export function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!date) return ''
  const d = new Date(date)
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const hours = String(d.getHours()).padStart(2, '0')
  const minutes = String(d.getMinutes()).padStart(2, '0')
  const seconds = String(d.getSeconds()).padStart(2, '0')
  
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

// 导出默认配置
export default {
  elementPlusConfig,
  breakpoints,
  spacing,
  borderRadius,
  shadows,
  gradients,
  isMobile,
  isTablet,
  isDesktop,
  getDeviceType,
  debounce,
  throttle,
  smoothScrollTo,
  formatNumber,
  formatFileSize,
  getImagePlaceholder,
  checkImageLoad,
  copyToClipboard,
  downloadFile,
  getRandomColor,
  generateId,
  formatDate,
}


